import {createRouter, createWebHashHistory} from 'vue-router'
import Home from '@/components/Home.vue'

const routes = [
    {
        name:'home',
        path:'/',
        meta:{
            title:'首页'
        },
        component:Home,
        redirect:'/login',
        children:[
            {
                name:'welcome',
                path:'/welcome',
                meta:{
                    title:'欢迎页'
                },
                component:()=>import('@/views/Welcome.vue')
            },
            {
                name:'user',
                path:'/system/user',
                meta:{
                    title:'用户管理'
                },
                component:()=>import('@/views/User.vue')
            },
            {
                name: 'group',
                path: '/system/group',
                meta: {
                    title: '组织管理'
                },
                component: () => import('@/views/Group.vue')
            },
            {
                name: 'menu',
                path: '/system/menu',
                meta: {
                    title: '菜单管理'
                },
                component: () => import('@/views/Menu.vue')
            },
            {
                name: 'role',
                path: '/system/role',
                meta: {
                    title: '角色管理'
                },
                component: () => import('@/views/Role.vue')
            },
            {
                name: 'file',
                path: '/system/file',
                meta: {
                    title: '文件管理'
                },
                component: () => import('@/views/File.vue')
            },
            {
                name: 'application',
                path: '/system/application',
                meta: {
                    title: '应用管理'
                },
                component: () => import('@/views/Application.vue')
            },
            {
                name: 'online',
                path: '/system/online',
                meta: {
                    title: '在线用户'
                },
                component: () => import('@/views/Online.vue')
            },
            {
                name: 'loginLog',
                path: '/system/loginLog',
                meta: {
                    title: '登录日志'
                },
                component: () => import('@/views/LoginLog.vue')
            },
            {
                name: 'device',
                path: '/iot/device',
                meta: {
                    title: '设备管理'
                },
                component: () => import('@/views/Device.vue')
            },
            {
                name: 'model',
                path: '/iot/model',
                meta: {
                    title: '模型管理'
                },
                component: () => import('@/views/Model.vue')
            }
        ]
    },
    {
        name:'login',
        path:'/login',
        meta:{
            title:'登陆'
        },
        component:()=>import('@/views/Login.vue')
    },
    {
        name:'register',
        path:'/register',
        meta:{
            title:'注册'
        },
        component:()=>import('@/views/Register.vue')
    },
    {
        name:'downloadApp',
        path:'/downloadApp',
        meta:{
            title:''
        },
        component:()=>import('@/views/DownloadApp.vue')
    },
    {
        name:'404',
        path:'/404',
        meta:{
            title:'页面不存在'
        },
        component:()=>import('@/views/404.vue')
    }
]

const router = createRouter({
    history:createWebHashHistory(),
    routes
})

//判断当前地址是否可以访问
function checkPermission(path) {
    let hasPermission = router.getRoutes().filter(router => router.path == path).length;
    if(hasPermission){
        return true;
    }else{
        return false;
    }
}

//导航守卫
router.beforeEach((to,from,next)=>{
    if(checkPermission(to.path)){
        document.title = to.meta.title;
        next()
    }else{
        next('/404')
    }
})

export default router